package edu.predatorwars.server.host;

import java.util.UUID;
import java.util.List;
import edu.predatorwars.server.engine.GameEngine;
import edu.predatorwars.server.engine.GameListener;
import edu.predatorwars.shared.models.*;


public class GameInstance implements GameListener {

	private GameHostImpl host;
	private UUID gameId; 
	private GameEngine engine;
	
	public GameInstance(GameHostImpl host, UUID gameId, GameEngine engine) {
		this.host = host; //Needs reference to host in order to update it
		this.gameId = gameId; //unique identifier for the game
		this.engine = engine;
	}

	public UUID getUUID() {
		return gameId;
	}
	
	public List<Player> getPlayerList() {
		return engine.getStartingPlayers();
	}
	
	public GameEngine getGameEngine() {
		return engine;
	}

	
	public void boardChanged(GameEngine engine, Board board) {
		//create message payload, redirect to host
		
		/*assertion: engine.getCurrentPlayer.STATUS == ALIVE
		if(engine.getCurrentPlayer().getPlayerStatus() != PlayerStatus.ALIVE) {
			throw new Exception("Player whose turn it is must be alive");
		}*/
		if (engine.getGameState() == 3) {//game ended 
			return;
		}

		BoardPayload payload = new BoardPayload(board, engine.getStartingPlayers(), engine.getCurrentPlayer(), gameId, engine.getBoardSnapshot());
		host.latestUpdate(payload, engine.getStartingPlayers());
	}
	
	
	public void gameStateChanged(GameEngine engine, int boardState) {
		//create message payload, redirect to host
		if (boardState == 1) {
			//character selection stage, different payload type
			CharacterListPayload payload = new CharacterListPayload(engine.getAvailableCreatures(), gameId);
			host.latestUpdate(payload, engine.getStartingPlayers());
		} else if (boardState == 3) { //game has ended
			StateChangePayload payload = new StateChangePayload(boardState, engine.getStartingPlayers(), gameId);
			host.gameEnding(gameId, payload, engine.getStartingPlayers());
			
		} else {
			StateChangePayload payload = new StateChangePayload(boardState, engine.getStartingPlayers(), gameId);
			host.latestUpdate(payload, engine.getStartingPlayers());
		}
	}
}
